﻿using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
using UnityEngine.UI;

namespace GUITree
{
	// Token: 0x020004B7 RID: 1207
	[ExecuteInEditMode]
	[RequireComponent(typeof(RectTransform))]
	[AddComponentMenu("GUITree/Text Size Fitter", 1002)]
	public class TextSizeFitter : UIBehaviour, ITreeLayoutElement, ILayoutGroup, ILayoutElement, ILayoutController
	{
		// Token: 0x06001E3E RID: 7742 RVA: 0x000BEFAC File Offset: 0x000BD1AC
		protected TextSizeFitter()
		{
			if (this.m_Padding == null)
			{
				this.m_Padding = new RectOffset();
			}
		}

		// Token: 0x17000344 RID: 836
		// (get) Token: 0x06001E3F RID: 7743 RVA: 0x000BF010 File Offset: 0x000BD210
		// (set) Token: 0x06001E40 RID: 7744 RVA: 0x000BF018 File Offset: 0x000BD218
		public RectOffset padding
		{
			get
			{
				return this.m_Padding;
			}
			set
			{
				this.SetProperty<RectOffset>(ref this.m_Padding, value);
			}
		}

		// Token: 0x17000345 RID: 837
		// (get) Token: 0x06001E41 RID: 7745 RVA: 0x000BF028 File Offset: 0x000BD228
		// (set) Token: 0x06001E42 RID: 7746 RVA: 0x000BF030 File Offset: 0x000BD230
		public TextAnchor childAlignment
		{
			get
			{
				return this.m_ChildAlignment;
			}
			set
			{
				this.SetProperty<TextAnchor>(ref this.m_ChildAlignment, value);
			}
		}

		// Token: 0x17000346 RID: 838
		// (get) Token: 0x06001E43 RID: 7747 RVA: 0x000BF040 File Offset: 0x000BD240
		protected RectTransform rectTransform
		{
			get
			{
				if (this.m_Rect == null)
				{
					this.m_Rect = base.GetComponent<RectTransform>();
				}
				return this.m_Rect;
			}
		}

		// Token: 0x17000347 RID: 839
		// (get) Token: 0x06001E44 RID: 7748 RVA: 0x000BF068 File Offset: 0x000BD268
		protected Text text
		{
			get
			{
				if (this.m_Text == null)
				{
					this.m_Text = base.GetComponentInChildren<Text>();
				}
				return this.m_Text;
			}
		}

		// Token: 0x17000348 RID: 840
		// (get) Token: 0x06001E45 RID: 7749 RVA: 0x000BF090 File Offset: 0x000BD290
		protected RectTransform rectText
		{
			get
			{
				if (this.m_RectText == null && this.text != null)
				{
					this.m_RectText = this.text.rectTransform;
				}
				return this.m_RectText;
			}
		}

		// Token: 0x17000349 RID: 841
		// (get) Token: 0x06001E46 RID: 7750 RVA: 0x000BF0CC File Offset: 0x000BD2CC
		protected ILayoutElement elementText
		{
			get
			{
				if (this.m_ElementText == null && this.text != null)
				{
					this.m_ElementText = this.text.GetComponent<ILayoutElement>();
				}
				return this.m_ElementText;
			}
		}

		// Token: 0x1700034A RID: 842
		// (get) Token: 0x06001E47 RID: 7751 RVA: 0x000BF104 File Offset: 0x000BD304
		// (set) Token: 0x06001E48 RID: 7752 RVA: 0x000BF10C File Offset: 0x000BD30C
		private bool isContentSizeFitter { get; set; }

		// Token: 0x06001E49 RID: 7753 RVA: 0x000BF118 File Offset: 0x000BD318
		public void CalculateLayoutInputHorizontal()
		{
			this.m_Tracker.Clear();
			ContentSizeFitter component = this.rectTransform.GetComponent<ContentSizeFitter>();
			this.isContentSizeFitter = (component != null);
			if (this.isContentSizeFitter)
			{
				this.m_FitModeHorizontal = component.horizontalFit;
				this.m_FitModeVertical = component.verticalFit;
			}
			this.CalcAlongAxis(0);
		}

		// Token: 0x06001E4A RID: 7754 RVA: 0x000BF174 File Offset: 0x000BD374
		public void CalculateLayoutInputVertical()
		{
			this.CalcAlongAxis(1);
		}

		// Token: 0x1700034B RID: 843
		// (get) Token: 0x06001E4B RID: 7755 RVA: 0x000BF180 File Offset: 0x000BD380
		public virtual float minWidth
		{
			get
			{
				return this.GetTotalMinSize(0);
			}
		}

		// Token: 0x1700034C RID: 844
		// (get) Token: 0x06001E4C RID: 7756 RVA: 0x000BF18C File Offset: 0x000BD38C
		public virtual float preferredWidth
		{
			get
			{
				return this.GetTotalPreferredSize(0);
			}
		}

		// Token: 0x1700034D RID: 845
		// (get) Token: 0x06001E4D RID: 7757 RVA: 0x000BF198 File Offset: 0x000BD398
		public virtual float flexibleWidth
		{
			get
			{
				return this.GetTotalFlexibleSize(0);
			}
		}

		// Token: 0x1700034E RID: 846
		// (get) Token: 0x06001E4E RID: 7758 RVA: 0x000BF1A4 File Offset: 0x000BD3A4
		public virtual float minHeight
		{
			get
			{
				return this.GetTotalMinSize(1);
			}
		}

		// Token: 0x1700034F RID: 847
		// (get) Token: 0x06001E4F RID: 7759 RVA: 0x000BF1B0 File Offset: 0x000BD3B0
		public virtual float preferredHeight
		{
			get
			{
				return this.GetTotalPreferredSize(1);
			}
		}

		// Token: 0x17000350 RID: 848
		// (get) Token: 0x06001E50 RID: 7760 RVA: 0x000BF1BC File Offset: 0x000BD3BC
		public virtual float flexibleHeight
		{
			get
			{
				return this.GetTotalFlexibleSize(1);
			}
		}

		// Token: 0x17000351 RID: 849
		// (get) Token: 0x06001E51 RID: 7761 RVA: 0x000BF1C8 File Offset: 0x000BD3C8
		public virtual int layoutPriority
		{
			get
			{
				return int.MaxValue;
			}
		}

		// Token: 0x06001E52 RID: 7762 RVA: 0x000BF1D0 File Offset: 0x000BD3D0
		public void SetLayoutHorizontal()
		{
			this.SetChildrenAlongAxis(0);
		}

		// Token: 0x06001E53 RID: 7763 RVA: 0x000BF1DC File Offset: 0x000BD3DC
		public void SetLayoutVertical()
		{
			this.SetChildrenAlongAxis(1);
		}

		// Token: 0x06001E54 RID: 7764 RVA: 0x000BF1E8 File Offset: 0x000BD3E8
		protected override void OnEnable()
		{
			base.OnEnable();
			this.SetDirty();
		}

		// Token: 0x06001E55 RID: 7765 RVA: 0x000BF1F8 File Offset: 0x000BD3F8
		protected override void OnDisable()
		{
			this.m_Tracker.Clear();
			if (this.rectTransform != null)
			{
				LayoutRebuilder.MarkLayoutForRebuild(this.rectTransform);
			}
			base.OnDisable();
		}

		// Token: 0x06001E56 RID: 7766 RVA: 0x000BF228 File Offset: 0x000BD428
		protected override void OnDidApplyAnimationProperties()
		{
			this.SetDirty();
		}

		// Token: 0x17000352 RID: 850
		// (get) Token: 0x06001E57 RID: 7767 RVA: 0x000BF230 File Offset: 0x000BD430
		// (set) Token: 0x06001E58 RID: 7768 RVA: 0x000BF238 File Offset: 0x000BD438
		public bool childForceExpandWidth
		{
			get
			{
				return this.m_ChildForceExpandWidth;
			}
			set
			{
				this.SetProperty<bool>(ref this.m_ChildForceExpandWidth, value);
			}
		}

		// Token: 0x17000353 RID: 851
		// (get) Token: 0x06001E59 RID: 7769 RVA: 0x000BF248 File Offset: 0x000BD448
		// (set) Token: 0x06001E5A RID: 7770 RVA: 0x000BF250 File Offset: 0x000BD450
		public bool childForceExpandHeight
		{
			get
			{
				return this.m_ChildForceExpandHeight;
			}
			set
			{
				this.SetProperty<bool>(ref this.m_ChildForceExpandHeight, value);
			}
		}

		// Token: 0x06001E5B RID: 7771 RVA: 0x000BF260 File Offset: 0x000BD460
		protected void CalcAlongAxis(int axis)
		{
			float num = (float)((axis != 0) ? this.padding.vertical : this.padding.horizontal);
			float num2 = num;
			float num3 = num;
			float num4 = 0f;
			bool flag = axis == 1;
			float minSize = LayoutUtility.GetMinSize(this.elementText, axis);
			float preferredSize = LayoutUtility.GetPreferredSize(this.elementText, axis);
			float num5 = LayoutUtility.GetFlexibleSize(this.elementText, axis);
			if ((axis != 0) ? this.childForceExpandHeight : this.childForceExpandWidth)
			{
				num5 = Mathf.Max(num5, 1f);
			}
			if (flag)
			{
				num2 = Mathf.Max(minSize + num, num2);
				num3 = Mathf.Max(preferredSize + num, num3);
				num4 = Mathf.Max(num5, num4);
			}
			else
			{
				num2 += minSize;
				num3 += preferredSize;
				num4 += num5;
			}
			num3 = Mathf.Max(num2, num3);
			this.SetLayoutInputForAxis(num2, num3, num4, axis);
		}

		// Token: 0x06001E5C RID: 7772 RVA: 0x000BF340 File Offset: 0x000BD540
		protected void SetChildrenAlongAxis(int axis)
		{
			float num = this.rectTransform.rect.size[axis];
			bool flag = axis == 1;
			if (flag)
			{
				float value = num - (float)((axis != 0) ? this.padding.vertical : this.padding.horizontal);
				float minSize = LayoutUtility.GetMinSize(this.elementText, axis);
				float preferredSize = LayoutUtility.GetPreferredSize(this.elementText, axis);
				float num2 = LayoutUtility.GetFlexibleSize(this.elementText, axis);
				if ((axis != 0) ? this.childForceExpandHeight : this.childForceExpandWidth)
				{
					num2 = Mathf.Max(num2, 1f);
				}
				float num3 = Mathf.Clamp(value, minSize, (num2 <= 0f) ? preferredSize : num);
				float startOffset = this.GetStartOffset(axis, num3);
				this.SetChildAlongAxis(this.rectText, axis, startOffset, num3);
			}
			else
			{
				float num4 = (float)((axis != 0) ? this.padding.top : this.padding.left);
				if (this.GetTotalFlexibleSize(axis) == 0f && this.GetTotalPreferredSize(axis) < num)
				{
					num4 = this.GetStartOffset(axis, this.GetTotalPreferredSize(axis) - (float)((axis != 0) ? this.padding.vertical : this.padding.horizontal));
				}
				float t = 0f;
				if (this.GetTotalMinSize(axis) != this.GetTotalPreferredSize(axis))
				{
					t = Mathf.Clamp01((num - this.GetTotalMinSize(axis)) / (this.GetTotalPreferredSize(axis) - this.GetTotalMinSize(axis)));
				}
				float num5 = 0f;
				if (num > this.GetTotalPreferredSize(axis) && this.GetTotalFlexibleSize(axis) > 0f)
				{
					num5 = (num - this.GetTotalPreferredSize(axis)) / this.GetTotalFlexibleSize(axis);
				}
				float minSize2 = LayoutUtility.GetMinSize(this.elementText, axis);
				float preferredSize2 = LayoutUtility.GetPreferredSize(this.elementText, axis);
				float num6 = LayoutUtility.GetFlexibleSize(this.elementText, axis);
				if ((axis != 0) ? this.childForceExpandHeight : this.childForceExpandWidth)
				{
					num6 = Mathf.Max(num6, 1f);
				}
				float num7 = Mathf.Lerp(minSize2, preferredSize2, t);
				num7 += num6 * num5;
				this.SetChildAlongAxis(this.rectText, axis, num4, num7);
				num4 += num7;
			}
		}

		// Token: 0x06001E5D RID: 7773 RVA: 0x000BF59C File Offset: 0x000BD79C
		protected float GetTotalMinSize(int axis)
		{
			return this.m_TotalMinSize[axis];
		}

		// Token: 0x06001E5E RID: 7774 RVA: 0x000BF5AC File Offset: 0x000BD7AC
		protected float GetTotalPreferredSize(int axis)
		{
			return (this.isContentSizeFitter && !(this.isContentSizeFitter & ((axis != 0) ? this.m_FitModeVertical : this.m_FitModeHorizontal) == ContentSizeFitter.FitMode.PreferredSize)) ? this.rectTransform.sizeDelta[axis] : this.m_TotalPreferredSize[axis];
		}

		// Token: 0x06001E5F RID: 7775 RVA: 0x000BF610 File Offset: 0x000BD810
		protected float GetTotalFlexibleSize(int axis)
		{
			return this.m_TotalFlexibleSize[axis];
		}

		// Token: 0x06001E60 RID: 7776 RVA: 0x000BF620 File Offset: 0x000BD820
		protected float GetStartOffset(int axis, float requiredSpaceWithoutPadding)
		{
			float num = requiredSpaceWithoutPadding + (float)((axis != 0) ? this.padding.vertical : this.padding.horizontal);
			float num2 = this.rectTransform.rect.size[axis];
			float num3 = num2 - num;
			float num4=1;
			if (axis == 0)
			{
				//num4 = (float)(this.childAlignment % TextAnchor.MiddleLeft) * 0.5f;
			}
			else
			{
				//num4 = (float)(this.childAlignment / TextAnchor.MiddleLeft) * 0.5f;
			}
			return (float)((axis != 0) ? this.padding.top : this.padding.left) + num3 * num4;
		}

		// Token: 0x06001E61 RID: 7777 RVA: 0x000BF6D0 File Offset: 0x000BD8D0
		protected void SetLayoutInputForAxis(float totalMin, float totalPreferred, float totalFlexible, int axis)
		{
			this.m_TotalMinSize[axis] = totalMin;
			this.m_TotalPreferredSize[axis] = totalPreferred;
			this.m_TotalFlexibleSize[axis] = totalFlexible;
		}

		// Token: 0x06001E62 RID: 7778 RVA: 0x000BF6FC File Offset: 0x000BD8FC
		protected void SetChildAlongAxis(RectTransform rect, int axis, float pos, float size)
		{
			if (rect == null)
			{
				return;
			}
			this.m_Tracker.Add(this, rect, DrivenTransformProperties.AnchoredPositionX | DrivenTransformProperties.AnchoredPositionY | DrivenTransformProperties.AnchorMinX | DrivenTransformProperties.AnchorMinY | DrivenTransformProperties.AnchorMaxX | DrivenTransformProperties.AnchorMaxY | DrivenTransformProperties.SizeDeltaX | DrivenTransformProperties.SizeDeltaY);
			rect.SetInsetAndSizeFromParentEdge((axis != 0) ? RectTransform.Edge.Top : RectTransform.Edge.Left, pos, size);
		}

		// Token: 0x06001E63 RID: 7779 RVA: 0x000BF734 File Offset: 0x000BD934
		protected override void OnRectTransformDimensionsChange()
		{
			base.OnRectTransformDimensionsChange();
		}

		// Token: 0x06001E64 RID: 7780 RVA: 0x000BF73C File Offset: 0x000BD93C
		protected virtual void OnTransformChildrenChanged()
		{
			this.SetDirty();
		}

		// Token: 0x06001E65 RID: 7781 RVA: 0x000BF744 File Offset: 0x000BD944
		protected void SetProperty<T>(ref T currentValue, T newValue)
		{
			if ((currentValue == null && newValue == null) || (currentValue != null && currentValue.Equals(newValue)))
			{
				return;
			}
			currentValue = newValue;
			this.SetDirty();
		}

		// Token: 0x06001E66 RID: 7782 RVA: 0x000BF7A4 File Offset: 0x000BD9A4
		protected void SetDirty()
		{
			if (!this.IsActive())
			{
				return;
			}
			if (this.rectTransform != null)
			{
				LayoutRebuilder.MarkLayoutForRebuild(this.rectTransform);
			}
		}

		// Token: 0x04002092 RID: 8338
		[SerializeField]
		protected RectOffset m_Padding = new RectOffset();

		// Token: 0x04002093 RID: 8339
		[SerializeField]
		[FormerlySerializedAs("m_Alignment")]
		protected TextAnchor m_ChildAlignment;

		// Token: 0x04002094 RID: 8340
		[NonSerialized]
		private RectTransform m_Rect;

		// Token: 0x04002095 RID: 8341
		protected DrivenRectTransformTracker m_Tracker;

		// Token: 0x04002096 RID: 8342
		private Vector2 m_TotalMinSize = Vector2.zero;

		// Token: 0x04002097 RID: 8343
		private Vector2 m_TotalPreferredSize = Vector2.zero;

		// Token: 0x04002098 RID: 8344
		private Vector2 m_TotalFlexibleSize = Vector2.zero;

		// Token: 0x04002099 RID: 8345
		private Text m_Text;

		// Token: 0x0400209A RID: 8346
		private RectTransform m_RectText;

		// Token: 0x0400209B RID: 8347
		private ILayoutElement m_ElementText;

		// Token: 0x0400209D RID: 8349
		private ContentSizeFitter.FitMode m_FitModeHorizontal;

		// Token: 0x0400209E RID: 8350
		private ContentSizeFitter.FitMode m_FitModeVertical;

		// Token: 0x0400209F RID: 8351
		[SerializeField]
		protected bool m_ChildForceExpandWidth = true;

		// Token: 0x040020A0 RID: 8352
		[SerializeField]
		protected bool m_ChildForceExpandHeight = true;
	}
}
